﻿#region Copyright
//       Copyright (c) 2009 - Antoine Jaussoin - http://www.jaussoin.com/blog/
// 
//        This program is free software: you can redistribute it and/or modify
//        it under the terms of the GNU General Public License as published by
//        the Free Software Foundation, either version 3 of the License, or
//        (at your option) any later version.
// 
//        This program is distributed in the hope that it will be useful,
//        but WITHOUT ANY WARRANTY; without even the implied warranty of
//        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//        GNU General Public License for more details.
// 
//        You should have received a copy of the GNU General Public License
//        along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace FT.Architecture.Example.Console.Benchmark.AllPets
{
    public class AllPetsSqlBenchmark : BenchmarkBase
    {
        /// <summary>
        /// Return all the pets
        /// </summary>
        /// <returns></returns>
        protected override IList<BenchmarkItem> RunInternal(BenchmarkTimer timer)
        {
            IList<BenchmarkItem> results = new List<BenchmarkItem>();

            SqlConnection connection = new SqlConnection(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=ncontrols;Integrated Security=SSPI;");
            connection.Open();
            timer.ReportEvent("Connection opened");

            SqlCommand command = connection.CreateCommand();
            timer.ReportEvent("Command created");

            command.CommandText = "select * from Pet";

            using(var reader = command.ExecuteReader())
            {
                timer.ReportEvent("Reader executed");
                while(reader.Read())
                {
                    results.Add(new BenchmarkItem(reader[2],reader[0], reader[2]));
                }
            }

            timer.ReportEvent("Result collection done");

            command.Dispose();
            connection.Close();
            connection.Dispose();

            timer.ReportEvent("Dispose methods called");

            return results;
        }

        /// <summary>
        /// Code that is run before run
        /// </summary>
        protected override void BeforeRun()
        {
            
        }

        /// <summary>
        /// Code that is run after run
        /// </summary>
        protected override void AfterRun()
        {
            
        }

        /// <summary>
        /// Benchmark name
        /// </summary>
        public override string Name
        {
            get { return "All Pets SQL"; }
        }

        /// <summary>
        /// Benchmark description
        /// </summary>
        public override string Description
        {
            get { return "All the pets with plain old SQL"; }
        }
    }
}
